package com.valveinspectionsys.dbhelp;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * SQLite帮助类，建表及更新表，打开数据库
 * 
 * @author king
 * 
 */
public class DBOpenHelper extends SQLiteOpenHelper {
    
    private static final String Tag=DBOpenHelper.class.getName();

    public DBOpenHelper(Context context,String sqlName) {
	super(context, sqlName, null, 8);
    }

    /**
     * 建表
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
       
	String deviceinfo = "create table deviceinfo (_id  INTEGER PRIMARY KEY AUTOINCREMENT,deviceid varchar(20),devicenumber varchar(20),"
		+ "latitude number,longitude number,deviceadds varchar(20),RFIDnumber varchar(20),devicelistid varchar(20),copydata varchar(20),"
		+ "devicecaliber varchar(20),Navigationupload varchar(10),copydate varchar(20),useStatus varchar(20),openStatus varchar(20),devicelib varchar(20)"
		+ ",bandingstatus varchar(10),iscopystatus varchar(10),isupload varchar(10),isnorfid varchar(10),Navigationdownload varchar(10),devicesort number" +
		",devicePressure varchar(20),inspectionCycle varchar(20),maintenanceCycle varchar(20),producers varchar(20),place varchar(20),area varchar(20),netWork varchar(20)," +
		"productionDate varchar(20),InstallationDate varchar(20),devicetype varchar(20),operationtype varchar(10),remark varchar(100),deviceitem varchar(20))";
	Log.e(Tag, deviceinfo + "设备");
	db.execSQL(deviceinfo);

        
	String ordertypeSql = "create table type(_id  INTEGER PRIMARY KEY AUTOINCREMENT,number varchar(20),name varchar(20),status varchar(20),copyerid varchar(10))";
	Log.e(Tag, ordertypeSql+"类型");
	db.execSQL(ordertypeSql);

	String ResourcesSql = "create table resources(_id  INTEGER PRIMARY KEY AUTOINCREMENT,otherid number,path varchar(40),type varchar(10),status varchar(10),copyerid varchar(10),devicelistid varchar(20))";
	db.execSQL(ResourcesSql);
	
	Log.e(Tag, ResourcesSql+"资源");
	String devicelist = "create table devicelist(_id INTEGER PRIMARY KEY AUTOINCREMENT, devicelistid varchar(20),number varchar(20),name varchar(20),copyerid varchar(10),deviceitem varchar(20),operationtype varchar(20),rounttype varchar(10))";
	db.execSQL(devicelist);
	Log.d(Tag,"-------------设备册-------"+devicelist);

        String deviceItem="create table deviceItem(_id INTEGER PRIMARY KEY AUTOINCREMENT,number varchar(20),name varchar(20),forcedpictures varchar(20),type varchar(20))";
	db.execSQL(deviceItem);
	
	Log.d(Tag,"-------------设备巡检项目-------"+deviceItem);
	
	String devicesubitem="create table devicesubitem(_id INTEGER PRIMARY KEY AUTOINCREMENT,number varchar(20),name varchar(20),itemNumber varchar(20))";
        db.execSQL(devicesubitem);
        
        Log.d(Tag,"-------------设备巡检项目的字项目-------"+devicesubitem);
        
        String devicelist_item="create table devicelist_item(_id INTEGER PRIMARY KEY AUTOINCREMENT,devicelistid varchar(20),deviceitemid varchar(20) )";
        db.execSQL(devicelist_item);
        Log.d(Tag, devicelist_item+"设备册与巡检项目中间表册");
        
        String graphicnavigation = "create table graphicnavigation(_id INTEGER PRIMARY KEY AUTOINCREMENT,imgpath varchar(20),imgwebname varchar(20),"
		+ "type varchar(20),othernumber varchar(20) ,status varchar(20),isdownload varchar(20),Sortnumber number,isimg varchar(20),waterlistid varchar(10),isupload varchar(10) )";
	db.execSQL(graphicnavigation);
	System.out.println("图文导航" + graphicnavigation);
	Log.d(Tag, graphicnavigation+"资源导航");
	
	String repairSingle="create table repairSingle(_id INTEGER PRIMARY KEY AUTOINCREMENT,repairmethod varchar(20),faulttype varchar(20)," +
			"replacement  varchar(20),deviceid varchar(20),repairinfo varchar(20),remark varchar(50),devicelistid varchar(10)," +
			"repairtype varchar(10),repairdate varchar(20),repairstatus varchar(20),isupload varchar(10) ,isimage varchar(10))";
	db.execSQL(repairSingle);
	Log.d(Tag, repairSingle+"维修信息");
	
	String engineeringOrderSql = "create table engineeringOrder(_id  INTEGER PRIMARY KEY AUTOINCREMENT,deviceid varchar(20),ordertype varchar(20),"
		+ "desc varchar(100),isimg varchar(20),videopath varchar(20),soundpath varchar(20),ordername varchar(20),status varchar(20),copyerid varchar(10) ,latitude number,longitude number)";
	System.out.println(engineeringOrderSql + "工程单");
	db.execSQL(engineeringOrderSql);

    }

    /**
     * 当前版本号高于上次创建或升级的版本号，SQLiteOpenHelper会调用onUpdate()方法
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	db.execSQL("DROP TABLE IF EXISTS deviceinfo");
	db.execSQL("DROP TABLE IF EXISTS type");	
	db.execSQL("DROP TABLE IF EXISTS resources");
	db.execSQL("DROP TABLE IF EXISTS devicelist");
	db.execSQL("DROP TABLE IF EXISTS deviceItem");
	db.execSQL("DROP TABLE IF EXISTS graphicnavigation");
	db.execSQL("DROP TABLE IF EXISTS devicesubitem");
	db.execSQL("DROP TABLE IF EXISTS devicelist_item");
	db.execSQL("DROP TABLE IF EXISTS repairSingle");
	db.execSQL("DROP TABLE IF EXISTS engineeringOrder");
	onCreate(db);
    }

}
